import { Bot } from "./Bot.js";
import mineflayer from "mineflayer";

function _idleLook(bot: Bot): void {
  const mcBot: mineflayer.Bot = bot._bot;
  const entity = mcBot.nearestEntity();
  let entity_in_view =
    entity && entity.position.distanceTo(mcBot.entity.position) < 12 && entity.name !== "enderman";
  if (entity_in_view && entity && Math.random() < 0.9) {
    const height =
      entity.type !== "player" && entity.metadata[16] ? entity.height / 2 : entity.height;
    mcBot.lookAt(entity.position.offset(0, height, 0));
  } else {
    mcBot.look(Math.random() * Math.PI * 2, (Math.random() * Math.PI) / 2 - Math.PI / 4);
  }
}
export function idleHandler(bot: Bot): void {
  _idleLook(bot);
  bot._idleNextChange = Date.now() + Math.random() * 5000 + 1000;
}
